package com.yzk.aigy.controller;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yzk.aigy.common.Result;
import com.yzk.aigy.model.system.SysCategory;
import com.yzk.aigy.model.vo.SysCategoryQueryVo;
import com.yzk.aigy.service.SysCategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Api(tags = "影视分类管理")
@RequiredArgsConstructor
@RequestMapping("/admin/system/sysCategory")
public class SysCategoryController {

    private final SysCategoryService sysCategoryService;

    @ApiOperation("获取全部分类")
    @GetMapping("/findAll")
    public Result<List<SysCategory>> findAll() {
        return Result.ok(sysCategoryService.list());
    }

    @ApiOperation("分类分页查询")
    @PostMapping("/page")
    public Result<IPage<SysCategory>> findCategoryByPageQuery(@RequestBody SysCategoryQueryVo sysCategoryQueryVo) {
        IPage<SysCategory> page = new Page<>(sysCategoryQueryVo.getPageNum(), sysCategoryQueryVo.getPageSize());
        Wrapper<SysCategory> wrapper = Wrappers.<SysCategory>lambdaQuery()
                .like(StringUtils.hasText(sysCategoryQueryVo.getName()), SysCategory::getName, sysCategoryQueryVo.getName())
                .orderByDesc(SysCategory::getId);
        sysCategoryService.page(page, wrapper);
        return Result.ok(page);
    }

    @ApiOperation("添加分类")
    @PostMapping("/addCategory")
    public Result<Object> addCategory(@RequestBody SysCategory sysCategory) {
        return sysCategoryService.save(sysCategory) ? Result.ok() : Result.fail();
    }

    @ApiOperation("根据ID查询分类")
    @GetMapping("/findCategoryById/{id}")
    public Result<SysCategory> findCategoryById(@PathVariable Long id) {
        return Result.ok(sysCategoryService.getById(id));
    }

    @ApiOperation("修改分类")
    @PostMapping("/updateCategory")
    public Result<Object> updateCategory(@RequestBody SysCategory sysCategory) {
        return sysCategoryService.updateById(sysCategory) ? Result.ok() : Result.fail();
    }

    @ApiOperation("根据ID移除分类")
    @DeleteMapping("/removeCategory/{id}")
    public Result<Object> removeCategory(@PathVariable Long id) {
        return sysCategoryService.removeById(id) ? Result.ok() : Result.fail();
    }

    @ApiOperation("批量删除分类")
    @DeleteMapping("/removeCategoryByIds")
    public Result<Object> removeCategoryByIds(@RequestBody List<Long> ids) {
        return sysCategoryService.removeByIds(ids) ? Result.ok() : Result.fail();
    }
}
